
<template>
  <div class="app-container" v-cloak>
    <el-row :gutter="20">
      <!--      &lt;!&ndash;部门数据&ndash;&gt;
            <el-col :span="4" :xs="24">
              <div class="head-container">
                <el-input
                  v-model="deptName"
                  placeholder="请输入部门名称"
                  clearable
                  size="small"
                  prefix-icon="el-icon-search"
                  style="margin-bottom: 20px"
                />
              </div>
              <div class="head-container">
                <el-tree
                  :data="deptOptions"
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  :filter-node-method="filterNode"
                  ref="tree"
                  default-expand-all
                  @node-click="handleNodeClick"
                />
              </div>
            </el-col>-->
      <table>
        <tr>
          <td><span class="red">*</span><span>文档名称：</span></td>
          <td><el-input v-model="form.file_title" placeholder="请输入文档标题" style="width: 240px"></el-input></td>

        </tr>
        <br>
        <tr>
          <td>标签：</td>
          <td><el-input v-model="form.file_label" placeholder="请输入文档标签" style="width: 240px"></el-input></td>
        </tr>
        <br>
        <tr>
          <td>封面：</td>
          <td><!--<el-upload class="avatar-uploader"
                         :action="''"
                         :auto-upload="false"
                         :show-file-list="false"
                         :on-change="handleAccessoryChangeIcon">
            <img v-if="form.accessory" :src="form.accessory" class="avatar" style="height: 150px;">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>-->
            <el-upload
              :action="upload.url"
              :headers="headers"
              :on-success="handleSuccess"
              :file-list="photoList"
              list-type="picture-card"
              :auto-upload="true"
              ref="pictureUpload">
              <i slot="default" class="el-icon-plus"></i>
              <div slot="file" slot-scope="{file}">
                <img
                  class="el-upload-list__item-thumbnail"
                  :src="file.url" alt=""
                >
                <span class="el-upload-list__item-actions">
        <span
          class="el-upload-list__item-preview"
          @click="handlePictureCardPreview(file)"
        >
          <i class="el-icon-zoom-in"></i>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click="handleDownload(file)"
        >
          <i class="el-icon-download"></i>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click="handleRemove(file)"
        >
          <i class="el-icon-delete"></i>
        </span>
      </span>
              </div>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>

          </td>
        </tr>
        <br>
        <tr >
          <td><span class="red">*</span><span>所属频道：</span></td>
          <td  style="position:absolute;z-index: 2">
            <el-select v-model="queryParams3.parent_id" placeholder="请选择" @change="getListChannle1">
              <!--<el-option

                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>-->
              <el-option
                v-for="item in option"
                :key="item.channel_id"
                :label="item.channel_name"
                :value="item.channel_id"
              ></el-option>
            </el-select>

            <el-select v-model="form.channel_id" placeholder="请选择" style="margin-left: 20px">
              <!--<el-option

                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>-->
              <el-option
                v-for="item in option1"
                :key="item.channel_id"
                :label="item.channel_name"
                :value="item.channel_id"
              ></el-option>
            </el-select>
          </td>
        </tr>
        <br>
        <tr>
          <td>状态：</td>
          <td>
            <el-tag type="primary" effect="dark"  v-if="form.status==1">待评审</el-tag>
            <el-tag type="success" effect="dark"  v-if="form.status==2">评审通过</el-tag>
            <el-tag type="warning" effect="dark"  v-if="form.status==3">评审未通过</el-tag>
          </td>
        </tr>
        <br>
        <br>
        <tr>
          <td><span class="red">*</span><span>内容：</span></td>
          <td>
            <editor style="position: relative;z-index: 0;" ref="editorOne" v-model="form.file_content" ></editor>
          </td>

        </tr>
        <br>
        <tr>
          <td><span class="red">*</span><span>模板：</span></td>
          <td  style="position: absolute;z-index: 10001;text-align: center">

            <el-select v-model="form.template_id" placeholder="请选择">
              <!--<el-option

                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>-->
              <el-option
                v-for="item in templateOption"
                :key="item.template_id"
                :label="item.template_name"
                :value="item.template_id"
              ></el-option>
            </el-select>
          </td>
        </tr>
        <br>
        <tr>
          <td><span>描述：</span></td>
          <td>
            <!-- v-model="form.describe"-->
            <el-input
              style="width: 420px"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              placeholder="请输入内容"
              v-model="form.file_describe">
            </el-input>
          </td>

        </tr>
        <br>
        <tr>
          <td><span>外链：</span></td>
          <td>
            <!-- v-model="form.describe"-->
            <el-input v-model="form.url" placeholder="请输入文档外链地址" style="width: 360px"></el-input>
          </td>

        </tr>
        <br>
        <tr>
          <td><span>来源：</span></td>
          <td>
            <!-- v-model="form.describe"-->
            <el-input v-model="form.add_from" placeholder="请输入文档来源" style="width: 240px"></el-input>
          </td>

        </tr>
        <br>
        <tr>
          <td><span>作者：</span></td>
          <td>
            <!-- v-model="form.describe"-->
            <el-input v-model="form.author" placeholder="请输入文档作者" style="width: 240px"></el-input>
          </td>

        </tr>
        <br>
        <tr>
          <td>附件：</td>
          <td><el-upload
            class="upload-demo"
            :headers="headers"
            ref="upload"
            :on-success="success"
            :action="uploadFile.url"
            :on-preview="handlePreview"
            :on-remove="handleRemoveFile"
            :file-list="fileList"
            :auto-upload="true">
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            <!--<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>-->
            <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
          </el-upload></td>
        </tr>
        <br>
        <tr>
          <td><span>排序：</span></td>
          <td>
            <!-- v-model="form.describe"-->
            <el-input-number v-model="form.rank"></el-input-number>
          </td>

        </tr>
        <br>
        <tr>
          <td></td>
          <td>
            <el-button type="primary" @click="submit">提交</el-button>
            <el-button type="info" @click="reset" style="margin-left: 30px;">重置</el-button>
            <el-button type="success" @click="pass" style="margin-left: 30px;">通过审核</el-button>
            <el-button type="danger" @click="refuse" style="margin-left: 30px;">未审核</el-button></td>
        </tr>
      </table>

    </el-row>
  </div>
</template>

<script>

  import { listFile , getFile , addFile , updateFile ,delFile ,uploadFileImg ,updateFileStatus ,updateFileEdit} from "@/api/monitor/file";
  import { listChannel , getChannel , addChannel , updateChannel ,delChannel,getParentChannel} from "@/api/monitor/channel";
  import { getToken } from "@/utils/auth";
  import { listTemplate } from "@/api/monitor/template";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import Editor from '@/components/wangEditor'

  export default {
    name: "File",
    components: { Treeselect , Editor},
    data() {
      return {
        photoList:[],
        isClear: false,
        detail:"",
        user: {},
        sexs: [],
        option:[],
        id_card_type: [],
        status:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 默认密码
        initPassword: undefined,
        // 日期范围
        dateRange: [],
        // 状态数据字典
        statusOptions: [],
        // 性别状态字典
        Options: [],
        // 岗位选项
        postOptions: [],
        // 角色选项
        roleOptions: [],
        // 表单参数
        form: {
          accessory_file:""
        },
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 用户导入参数
        upload: {
          url: process.env.VUE_APP_BASE_API + "/uploadFileImg",

        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 1000,
          name: undefined,
          status: undefined,
          parent_id:'0'
        },
        /*        // 表单校验
                rules: {
                  userName: [
                    { required: true, message: "用户名称不能为空", trigger: "blur" }
                  ],
                  nickName: [
                    { required: true, message: "用户昵称不能为空", trigger: "blur" }
                  ],

                  email: [
                    {
                      type: "email",
                      message: "'请输入正确的邮箱地址",
                      trigger: ["blur", "change"]
                    }
                  ],
                  phonenumber: [
                    {
                      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                      message: "请输入正确的手机号码",
                      trigger: "blur"
                    }
                  ]
                }*/
        // 查询参数
        queryParams1: {
          pageNum: 1,
          pageSize: 1000,
        },
        uploadFile: {
          url: process.env.VUE_APP_BASE_API + "/uploadAccessoryFile",

        },
        fileList: [],
        // 日期范围
        dateRange1: [],
        templateOption:[],
        queryParams3: {
          pageNum: 1,
          pageSize: 1000,
          parent_id:undefined
        },
        option1:[],
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        params:{}
      };
    },
    computed:{
      headers(){
        return{
          token: getToken()
        }
      }
    },
    created() {

      const fileId = this.$route.params && this.$route.params.fileId;
      this.getFile(fileId);

      listTemplate(this.addDateRange(this.queryParams1, this.dateRange1)).then(response => {
          this.templateOption = response.data.items;
        }
      );
    },

    methods: {
      success(res){  //上传文件成功的回调函数
        console.log(res)
        this.form.accessory_file =this.form.accessory_file +  res.data[0].url +",";
        console.log(this.form.accessory_file)
      },
      submitUpload() {
        this.$refs.upload.submit();
      },
      handleRemoveFile(file, fileList) {
        var name = file.name;
        var strings = this.form.accessory_file.split(",")
        for (let i = 0; i <strings.length; i++) {
          if (strings[i]==name){
            strings.splice(i,1);
          }
        }

        var s = strings.toString()
        this.form.accessory_file = s;

        console.log(this.form.accessory_file);
      },
      handlePreview(file) {
        console.log(file);
      },
      getListChannle1(){
        listChannel(this.addDateRange(this.queryParams3, this.dateRange)).then(rs =>{
          this.option1 = rs.data.items;
        })
      },
      submit () {
        updateFileEdit(this.form).then(rs =>{
          this.msgSuccess("修改成功");
        })
      },
      // 表单重置
      reset() {
        this.form = {
          file_title: undefined,
          file_content: undefined,
          template_id: undefined,
          channel_id: undefined,
        };
        this.resetForm("form");
      },
      //获取频道
      async getListChannle(){
        try {
          let res = await listChannel(this.addDateRange(this.queryParams, this.dateRange));
          console.log(res)
          this.option = res.data.items;
          try {
            /*let res = await getChannel(this.form.channel_id);
            console.log(res)
            this.queryParams3.parent_id = res.data.parent_id;*/

          }catch (e) {
            console.log('getChannel-err')
            console.log(e)
          }
        }catch (e) {
          console.log('listChannel-err')
          console.log(e)
        }
        // listChannel(this.addDateRange(this.queryParams, this.dateRange)).then(rs =>{
        //   this.option = rs.data.items;
        //   try {
        //    let res = await getChannel(this.form.channel_id);
        //     console.log(res)
        //
        //     this.queryParams3.parent_id = res.data.parent_id;
        //     this.getListChannle1();
        //   }catch (e) {
        //     console.log(e)
        //   }
        //   console.log(this.option1)
        // })
      },
      getFile(fileId){
        getFile(fileId).then(rs =>{
          this.form = rs.data;
          let accessory = rs.data.accessory;
          if (accessory!=null && accessory!=undefined && accessory!=""){
            var list = accessory.split(",");
            /* console.log(list)*/
            for (let i = 0; i < list.length; i++) {
              this.photoList.push({name:list[i],url:list[i]})
            }
          }
          var accessoryFile = rs.data.accessory_file;
          if (accessoryFile!=null && accessoryFile!=undefined && accessoryFile!=""){
            var list = accessoryFile.split(",");
            for (let i = 0; i < list.length; i++) {
              if (list[i]!=null && list[i] != "" && list[i]!=undefined){
                this.fileList.push({name:list[i],url:list[i]})
              }
            }

          }
          this.queryParams3.parent_id = rs.data.parent_id
          this.getListChannle();
          this.getListChannle1();
        })
        /* console.log(this.form)*/

      },
      /*文档附件上传*/
      handleAccessoryChangeIcon(file, fileList) {//选中文件触发的change事件
        console.log(file)
        const isJPG = file.raw.type === 'image/jpeg'
        const isPNG = file.raw.type === 'image/png'
        // const isLt2M = file.raw.size / 1024 / 1024 < 0.5  //限制上传文件的大小
        if (!isPNG && !isJPG) {
          this.$message.error('上传图片只能是 JPG/PNG 格式!')
          return false
        } else {
          this.form.accessory = URL.createObjectURL(file.raw);//赋值图片的url，用于图片回显功能
          this.uploadImg(file)//调用上传文件api接口
          this.$forceUpdate()
        }
      },
      uploadImg(file){//文件上传操作
        let formData=new FormData()
        formData.append('file',file.raw)
        uploadFileImg(formData).then(res=>{
          console.log(res)
          if (res.code == '20000') {
            this.msgSuccess(res.data.msg);
            this.form.accessory = res.data[0].url;
            console.log(this.form.accessory)
          }
        })
      },
      //删除列表中的图片
      handleRemove(file) {
        // var url = file.response.data[0].url;
        var url = file.url;
        var accessory = this.form.accessory
        let strings = accessory.split(",");
        for (let i = 0; i < strings.length; i++) {
          if (strings[i] == url) {
            strings.splice(i,1);
          }
        }
        var rs = strings.join();
        this.form.accessory = rs;
        console.log(this.form.accessory )
        this.$refs.pictureUpload.handleRemove(file)
      },
      //预览图片
      handlePictureCardPreview(file) {
        console.log(file)
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      //下载图片
      handleDownload(file) {
        console.log(file);
      },
      //上传成功回调函数
      handleSuccess(res) {

        let accessory = this.form.accessory;
        if (accessory == undefined || accessory.length==0){
          this.form.accessory= res.data[0].url;
        } else {
          this.form.accessory = this.form.accessory+','+res.data[0].url;
        }
        console.log(this.form.accessory)
        /*this.$set(this.model, 'icon', res)*/
      },
      pass(){
        this.params.file_id=this.form.file_id;
        this.params.status = 2;
        updateFileStatus(this.params).then(res =>{
          this.msgSuccess("评审通过");
        })
      },
      refuse(){
        this.params.file_id=this.form.file_id;
        this.params.status = 0;
        updateFileStatus(this.params).then(res =>{
          this.msgSuccess("评审不通过");
        })
      },
    }

  };
</script>
<style>
  .myQuillEditor{
    height: 300px;
  }
  [v-cloak]{
    display: none !important;
  }
</style>
